//备注：拷贝代码请加上作者信息
//作者：王海涛
//邮箱：1126471088@qq.com
//版本：V1.0.0
/********************************************************
    说明：
    1、单片机内部flash的擦除与编程。
    2、擦除函数自带查空操作。
    3、编程函数自带校验操作。
    4、注意内部flash擦除的时候所有中断会暂停，小心看门狗导致重启现象。
**********************************************************/
#ifndef __FLASH_BSP_H__
#define __FLASH_BSP_H__

#include "gd32f4xx.h"
#include "gd32f4xx_fmc.h"
#include "../WWDG_BSP/wwdg_bsp.h"
#include "../IWDG_BSP/iwdg_bsp.h"

/*
STM32   F        429     V      E                 T     6
32bit   基础型号  基础款  引脚数  4:16KB LD小容量   封装  温度等级
                                6:32KB LD小容量
                                8:64KB MD中容量
                                B:128KB MD中容量
                                C:256KB HD大容量
                                E:512KB HD大容量
                                F:768KB XL超大容量
                                G:1024KB XL超大容量
                                H:1536KB
                                I:2048KB
*/

/*是否开启窗口看门狗*/
//#define WWDG_FUNC_ENABLE

typedef enum
{
    Flash_Sector_0  = CTL_SECTOR_NUMBER_0,  //16k字节
    Flash_Sector_1  = CTL_SECTOR_NUMBER_1,  //16k字节
    Flash_Sector_2  = CTL_SECTOR_NUMBER_2,  //16k字节
    Flash_Sector_3  = CTL_SECTOR_NUMBER_3,  //16k字节
    Flash_Sector_4  = CTL_SECTOR_NUMBER_4,  //64k字节
    Flash_Sector_5  = CTL_SECTOR_NUMBER_5,  //128k字节
    Flash_Sector_6  = CTL_SECTOR_NUMBER_6,  //128k字节
    Flash_Sector_7  = CTL_SECTOR_NUMBER_7,  //128k字节
    Flash_Sector_8  = CTL_SECTOR_NUMBER_8,  //128k字节
    Flash_Sector_9  = CTL_SECTOR_NUMBER_9,  //128k字节
    Flash_Sector_10 = CTL_SECTOR_NUMBER_10, //128k字节
    Flash_Sector_11 = CTL_SECTOR_NUMBER_11, //128k字节
    
    Flash_Sector_12 = CTL_SECTOR_NUMBER_12, //16k字节
    Flash_Sector_13 = CTL_SECTOR_NUMBER_13, //16k字节
    Flash_Sector_14 = CTL_SECTOR_NUMBER_14, //16k字节
    Flash_Sector_15 = CTL_SECTOR_NUMBER_15, //16k字节
    Flash_Sector_16 = CTL_SECTOR_NUMBER_16, //64k字节
    Flash_Sector_17 = CTL_SECTOR_NUMBER_17, //128k字节
    Flash_Sector_18 = CTL_SECTOR_NUMBER_18, //128k字节
    Flash_Sector_19 = CTL_SECTOR_NUMBER_19, //128k字节
    Flash_Sector_20 = CTL_SECTOR_NUMBER_20, //128k字节
    Flash_Sector_21 = CTL_SECTOR_NUMBER_21, //128k字节
    Flash_Sector_22 = CTL_SECTOR_NUMBER_22, //128k字节
    Flash_Sector_23 = CTL_SECTOR_NUMBER_23, //128k字节
    Flash_Sector_24 = CTL_SECTOR_NUMBER_24, //256k字节
    Flash_Sector_25 = CTL_SECTOR_NUMBER_25, //256k字节
    Flash_Sector_26 = CTL_SECTOR_NUMBER_26, //256k字节
    Flash_Sector_27 = CTL_SECTOR_NUMBER_27, //256k字节
}WHT_Flash_Sector_Number_enum;

typedef enum
{
    FLASH_Sector_0_Base_Address =  0x08000000, //16k字节
    FLASH_Sector_1_Base_Address =  0x08004000, //16k字节
    FLASH_Sector_2_Base_Address =  0x08008000, //16k字节
    FLASH_Sector_3_Base_Address =  0x0800C000, //16k字节
    FLASH_Sector_4_Base_Address =  0x08010000, //64k字节
    FLASH_Sector_5_Base_Address =  0x08020000, //128k字节
    FLASH_Sector_6_Base_Address =  0x08040000, //128k字节
    FLASH_Sector_7_Base_Address =  0x08060000, //128k字节
    FLASH_Sector_8_Base_Address =  0x08080000, //128k字节
    FLASH_Sector_9_Base_Address =  0x080A0000, //128k字节
    FLASH_Sector_10_Base_Address = 0x080C0000, //128k字节
    FLASH_Sector_11_Base_Address = 0x080E0000, //128k字节
    FLASH_Sector_12_Base_Address = 0x08100000, //16k字节
    FLASH_Sector_13_Base_Address = 0x08104000, //16k字节
    FLASH_Sector_14_Base_Address = 0x08108000, //16k字节
    FLASH_Sector_15_Base_Address = 0x0810C000, //16k字节
    FLASH_Sector_16_Base_Address = 0x08110000, //64k字节
    FLASH_Sector_17_Base_Address = 0x08120000, //128k字节
    FLASH_Sector_18_Base_Address = 0x08140000, //128k字节
    FLASH_Sector_19_Base_Address = 0x08160000, //128k字节
    FLASH_Sector_20_Base_Address = 0x08180000, //128k字节
    FLASH_Sector_21_Base_Address = 0x081A0000, //128k字节
    FLASH_Sector_22_Base_Address = 0x081C0000, //128k字节
    FLASH_Sector_23_Base_Address = 0x081E0000, //128k字节
    FLASH_Sector_24_Base_Address = 0x08200000, //256k字节
    FLASH_Sector_25_Base_Address = 0x08240000, //256k字节
    FLASH_Sector_26_Base_Address = 0x08280000, //256k字节
    FLASH_Sector_27_Base_Address = 0x082C0000, //256k字节
}WHT_Flash_Sector_Base_Address_enum;



/*擦除内部FLASH*/
extern ErrStatus WHT_Flash_BSP_Erase_And_Check(WHT_Flash_Sector_Number_enum start_sector_number, unsigned int sector_count);
/*读取内部FLASH,start_addr是真实地址*/
extern void WHT_Flash_BSP_Read(unsigned int start_addr, unsigned char* out_buf, unsigned int size);
/*写入内部FLASH，start_addr是真实地址*/
extern ErrStatus WHT_Flash_BSP_Write(unsigned int start_addr, const unsigned char* in_buf, unsigned int size);

#endif // !__FLASH_BSP_H__
